package com.example.tianling.admin.service.Impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tianling.admin.service.BookManageService;
import com.example.tianling.admin.vo.BookDataVo;
import com.example.tianling.dao.BookMapper;
import com.example.tianling.entity.BookEntity;
import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class bookManageServiceImpl
        extends ServiceImpl<BookMapper, BookEntity>
        implements BookManageService {
    @Autowired
    private BookMapper bookMapper;
//加载刷新论坛页面
    public Page<BookEntity> searchBook(Integer pageNum, Integer type, String keyword){
        QueryWrapper<BookEntity> queryWrapper = new QueryWrapper<>();
        if(type==0){
            queryWrapper.like("book_name",keyword);
        }
        else {
            queryWrapper.eq("book_type",type);
            queryWrapper.like("book_name",keyword);
        }
        queryWrapper.orderByDesc("year");
        Page<BookEntity> bookEntityPage = bookMapper.selectPage(new Page<BookEntity>(pageNum,10),queryWrapper);
        return bookEntityPage;
    }
    public BookEntity detailBook(String bookId){
        QueryWrapper<BookEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("booK_id",bookId);
        BookEntity bookEntity = bookMapper.selectOne(queryWrapper);
        return bookEntity;
    }

    public int updataBook(String bookId,String bookName,String year,String area,String bookType,String content){
        BookEntity bookEntity = new BookEntity();
        bookEntity.setBookId(Integer.valueOf(bookId));
        bookEntity.setBookName(bookName);
        bookEntity.setYear(Integer.valueOf(year));
        bookEntity.setArea(area);
        bookEntity.setType(bookType);
        bookEntity.setContent(content);
        return bookMapper.updateById(bookEntity);
    }

    public int deleteBook(List deleteList){
        QueryWrapper<BookEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("book_id",deleteList);
        return bookMapper.delete(queryWrapper);

    }

    public int insertBook(String bookName,String year,String area,String bookType,String content){
        BookEntity bookEntity = new BookEntity();
        bookEntity.setBookName(bookName);
        bookEntity.setYear(Integer.valueOf(year));
        bookEntity.setArea(area);
        bookEntity.setType(bookType);
        bookEntity.setContent(content);
        return bookMapper.insert(bookEntity);
    }

    public BookDataVo loadData(){

        QueryWrapper<BookEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("area",
                "SUM(CASE WHEN book_type = '1' THEN 1 ELSE 0 END) AS book_type1_count",
                "SUM(CASE WHEN book_type = '2' THEN 1 ELSE 0 END) AS book_type2_count",
                "SUM(CASE WHEN book_type = '3' THEN 1 ELSE 0 END) AS book_type3_count").groupBy("area");



        List<Map<String, Object>> area_data = bookMapper.selectMaps(queryWrapper);

//        System.out.println(area_data);
        List<Map<String, Object>> type_data = bookMapper.selectMaps(
                new QueryWrapper<BookEntity>()
                        .select("book_type","COUNT(*) AS count")
                        .groupBy("book_type")
        );

        return new BookDataVo(area_data,type_data);
    }
}
